#include "StdAfx.h"
#include "QueueBuffer.h"
#include "BufferConstraint.h"
#include "StreamPage.h"
#include <boost/shared_ptr.hpp>
QueueBuffer::QueueBuffer()
{

	this->freePositionHead.pageIndex=0;
	this->freePositionHead.chunkIndex=0;

	this->elementNumber = 0;
}


QueueBuffer::~QueueBuffer(void)
{
}




void QueueBuffer::enqueue(Element& element)
{
	assert(!isFull());
	this->setElementByMemoryPosition(element,freePositionHead);
	this->elementQueue.push(freePositionHead);
	this->getNextPosition(freePositionHead,freePositionHead);
	this->elementNumber++;
}
void QueueBuffer::dequeue(Element& element)
{
	assert(this->elementNumber>0);
	assert(elementQueue.size()>0);
	MemoryPosition dequePosition = this->elementQueue.front();
	this->getElementByMemoryPosition(element,dequePosition);
	this->elementNumber--;
	
	this->setNextPosition(dequePosition,freePositionHead);
	freePositionHead = dequePosition;
	this->elementQueue.pop();
}
void QueueBuffer::dequeue(void)
{
	assert(this->elementNumber>0);
	assert(elementQueue.size()>0);
	MemoryPosition dequePosition = this->elementQueue.front();
	this->elementNumber--;

	this->setNextPosition(dequePosition,freePositionHead);
	freePositionHead = dequePosition;
	this->elementQueue.pop();
}
void QueueBuffer::peek(Element& element)
{
	assert(this->elementNumber>0);

	MemoryPosition dequePosition = this->elementQueue.front();
	this->getElementByMemoryPosition(element,dequePosition);

}

bool QueueBuffer::isEmpty(void)
{
	return this->elementNumber==0;
}
bool QueueBuffer::isFull(void)
{
	bool ret;
	if(!isValidMemoryPosition(freePositionHead))
	{
		ret = this->allocateNewPage();
		if(ret == false)
		{
			return true;
		}
		this->freePositionHead.pageIndex = this->getPageNumber()-1;
		this->freePositionHead.chunkIndex = 0;

	}
	return false;
}
